package com.lovo.redis.controller;

import com.lovo.redis.bean.OrderBean;
import com.lovo.redis.config.RedisCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("redis")
public class RedisController {
    @Autowired
    RedisCacheUtil redisCacheUtil;

    @RequestMapping("add")
    public String add ( String name,Integer num, String movieName, Integer price, String date, String endDate){
        OrderBean orderBean = new OrderBean(name,num,movieName, price,date,endDate);
        orderBean.setId(String.valueOf(System.currentTimeMillis()));
        redisCacheUtil.hset("order",orderBean.getId(),orderBean);
        return "ok";
    }

    @RequestMapping("findAll")
    public List<Object> findAll(){
        List<Object> list = new ArrayList<>();
        Set<Object> set= redisCacheUtil.hashGetFileds("order");
        System.out.println(set);
        for (Object obj:set){
            list.add( redisCacheUtil.hget("order", String.valueOf(obj)));
            System.out.println(obj);
        }
        return list;
    }

    @RequestMapping("update")
    public String update( String key,String id, String name,Integer num,String movieName, Integer price, String date, String endDate){
        OrderBean orderBean = new OrderBean(name,num,movieName, price,date,endDate);
        orderBean.setId(id);
        redisCacheUtil.hdel("order", String.valueOf(key));
        redisCacheUtil.hset("order",key,orderBean);
        return "ok";
    }
    @RequestMapping("del")
    public String del(String key){
        System.out.println(key);
        redisCacheUtil.hdel("order",key);
        return "ok";
    }
}
